package cn.sccl.common.model;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;


@SuppressWarnings("serial")
public class TreeNode<T> extends BaseModel {

    protected String name;

    protected TreeNode<T> parent;

    protected List<T> children;


    // check it is root
    public boolean isRoot() {
        if (parent == null || parent.getId() == null || StringUtils.isBlank(parent.getId())) {
            return true;
        }
        return false;
    }


    // check it is leaf
    public boolean isLeaf() {
        if (children == null || children.isEmpty()) {
            return true;
        }
        return false;
    }


    // return the level of the node
//	public int getLevel()
//	{
//		return 0;
//	}


    @SuppressWarnings("unchecked")
    public void addChild(TreeNode<T> child) {
        if (child == null) {
            return;
        }
        if (children == null) {
            children = new ArrayList<T>();
        }
        if (!children.contains(child)) {
            children.add((T) child);
            child.setParent(this);
        }
    }


    public TreeNode<T> getParent() {
        return parent;
    }


    public void setParent(TreeNode<T> parent) {
        this.parent = parent;
    }


    public List<T> getChildren() {
        return children;
    }


    public void setChildren(List<T> children) {
        this.children = children;
    }


    public String getName() {
        if (StringUtils.isBlank(this.name)) {
            return "";
        }
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }
}


